/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 049. 从根节点到叶节点的路径数字之和
 * @author: WXY
 * @create: 2023-01-02 10:11
 * @Version 1.0
 **/
public class offer_sixteen3_sumNumbers {
    int sum = 0;

    public int sumNumbers(TreeNode root) {
        process(root, 0);
        return sum;
    }

    private void process(TreeNode root, int num) {
        if (root == null) return;
        num = num * 10 + root.val;
        if (root.left == null && root.right == null) {
            sum += num;
        }
        process(root.left, num);
        process(root.right, num);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
